function abs_pos(f) {
    if (f < 0) {
        f *= -1;
    }
    return f;
};
function update_streetnames(h/*GPS_Map*/) {
	return;
    var j = h.getZoom();
//    console.log("move the map....");
    if (j <= 13) {
        for (var i = 0; i < h.address_labels.length; i++) {
            var k = h.address_labels[i];
            k.hideMe();
        }
    } else {
//    	console.log("getting the address....");
        var l = h.getBounds();
        var m = "gps_get_address.jsp?action=streetnames&swlat=" + l.getSouthWest().lat() + "&swlng=" + l.getSouthWest().lng() + "&nelat=" + l.getNorthEast().lat() + "&nelng=" + l.getNorthEast().lng();
        GPS_DownloadUrl(m,
        function(a, b) {
            var c = a.split("~");
            var d = 0;
            for (var i = 0; i < c.length; i++) {
                var e = c[i];
                if (e == "") {
                    continue
                }
                var f = e.split("`");
                var g = null;
                if (d >= 100) {
                    d = 0;
                }
                if (d >= h.address_labels.length) {
                    g = new GPS_Label(new GPS_LatLng(f[0], f[1]));
                    h.addLabel(g);
                    h.address_labels.push(g);
                } else {
                    g = h.address_labels[d];
                }
                g.showMe();
                g.put_text('test adresss'+f[2]);
                g.move(new GPS_LatLng(f[0], f[1]));
                d++;
            }
        });
    }
};
var g_geocoder = null;
function get_address(k, l, m, trackerId) {
	if(g_geocoder == null)
		g_geocoder = new GPS_Geocoder();
    var n = "gps_get_address.jsp";
	Ext.Ajax.request({
	    url: n,
	    params: "action=getcache&lat=" + k + "&lng=" + l,
	    success:function(res){
		if (res.responseText == '') {
            return;
        }
		var f = res.responseText;
        var h = f.split("=");
        if (h.length < 2) {
            return;
        }
        switch (h[0]) {
        case "0": //  没有缓存,则保存到数据库，设置缓存
            var i = new GPS_LatLng(parseFloat(k), parseFloat(l)); 
            if(!g_geocoder) g_geocoder = new GPS_Geocoder();
            g_geocoder.getLocations(i,
            function(c, d) {
                m(c.lat(), c.lng(), d);
                var e = "gps_get_address.jsp?action=setaddress&i=2&lat=" + c.lat() + "&lng=" + c.lng() + "&address=" + encodeURI(encodeURI(d)+"&trackerId="+trackerId);
                Ext.Ajax.request({
            	    url: e,
            	    params: '',
            	    success:function(res){
                	
                }});
            });
            break;
        case "1"://  如果已经保存到数据库，则直接设置地址
            var j = h[1].split("`");
            m(j[0], j[1], j[2]);
            break;
        }
	}});
};
function load_streetnames(a/*GPS_Map*/) {
    var b = g_map.clearOverlays;  //  g_map = new GPS_Map()
    a.clearOverlays = function() {
        b;
        update_streetnames();
    };
    a.addListener("moveend",
    function() {
        update_streetnames(a);
    });
};
function parse_gprmc(a) {
    var b = new Array();
    var c = a.split(",");
    if (c.length <= 9) {
        return null;
    }
    b['signal'] = (c[2] == "A" ? 1 : 0);
    lat = c[3];
    lat_d = lat.substr(0, 2);
    lat_m = lat.substr(2);
    lng = c[5];
    lng_d = lng.substr(0, 3);
    lng_m = lng.substr(3);
    b['lat'] = parseFloat(parseFloat(lat_d) + parseFloat(lat_m) / 60);
    if (c[4] != "N") {
        b['lat'] = parseFloat(b['lat']) * ( - 1);
    }
    b['lat'] = parseFloat(b['lat'].toFixed(6));
    b['lng'] = parseFloat(parseFloat(lng_d) + parseFloat(lng_m) / 60);
    if (c[6] != "E") {
        b['lng'] = parseFloat(b['lng']) * ( - 1);
    }
    b['lng'] = parseFloat(b['lng'].toFixed(6));
    var d = c[9];
    var e = c[1];
    var f = new Date("20" + d.substr(4, 2), parseFloat(d.substr(2, 2)) - 1, d.substr(0, 2), e.substr(0, 2), e.substr(2, 2), e.substr(4, 2));
    f.setHours(f.getHours()+ g_default_time_zone_hours); // 调整时区
    f.setMinutes(f.getMinutes() + g_default_time_zone_minutes);
    
    var g = f.getFullYear() + "-" + (f.getMonth() + 1).GpsFormat('00') + "-" + f.getDate().GpsFormat('00');
    
    if(locale == 'en not use'){
    	g = (f.getMonth() + 1).GpsFormat('00') + "/" + f.getDate().GpsFormat('00')+"/"+f.getFullYear();
    }else if(locale == 'pt' || locale == 'en'){
    	g = f.getDate().GpsFormat('00')+ "/" + (f.getMonth() + 1).GpsFormat('00') + "/"+f.getFullYear();
    }
    b['date'] = g;
    b['time'] = f.getHours().GpsFormat('00') + ":" + f.getMinutes().GpsFormat('00') + ":" + f.getSeconds().GpsFormat('00');
    
    b['speed'] = c[7];
    if (b['speed'] == "") {
        b['speed'] = 0;
    }
   
    b['speed'] = parseFloat(b['speed']).toFixed(2);//parseFloat(parseFloat(b['speed']) * 1.85).toFixed(2);
    if (c[8] == "") {
        c[8] = 0;
    }
    b['direction'] = c[8];
    b['mileage'] =c[10];
    b['acc'] = c[11];
    b['door'] = c[12];
    return b;
};

function parse_gprmc_mao(a) {
    var b = new Array();
    var c = a.split(",");
    if (c.length <= 9) {
        return null;
    }
    b['signal'] = (c[2] == "A" ? 1 : 0);
    lat = c[3];
    lat_d = lat.substr(0, 2);
    lat_m = lat.substr(2);
    lng = c[5];
    lng_d = lng.substr(0, 3);
    lng_m = lng.substr(3);
    b['lat'] = parseFloat(parseFloat(lat_d) + parseFloat(lat_m) / 60);
    if (c[4] != "N") {
        b['lat'] = parseFloat(b['lat']) * ( - 1);
    }
    b['lat'] = parseFloat(b['lat'].toFixed(6));
    b['lng'] = parseFloat(parseFloat(lng_d) + parseFloat(lng_m) / 60);
    if (c[6] != "E") {
        b['lng'] = parseFloat(b['lng']) * ( - 1);
    }
    b['lng'] = parseFloat(b['lng'].toFixed(6));
    var d = c[9];
    var e = c[1];
    var f = new Date("20" + d.substr(4, 2), parseFloat(d.substr(2, 2)) - 1, d.substr(0, 2), e.substr(0, 2), e.substr(2, 2), e.substr(4, 2));
    f.setHours(f.getHours()+ g_default_time_zone_hours); // 调整时区
    f.setMinutes(f.getMinutes() + g_default_time_zone_minutes);
    
    var g = f.getFullYear() + "-" + (f.getMonth() + 1).GpsFormat('00') + "-" + f.getDate().GpsFormat('00');
    
    if(locale == 'en not use'){
    	g = (f.getMonth() + 1).GpsFormat('00') + "/" + f.getDate().GpsFormat('00')+"/"+f.getFullYear();
    }else if(locale == 'pt' || locale == 'en'){
    	g = f.getDate().GpsFormat('00')+ "/" + (f.getMonth() + 1).GpsFormat('00') + "/"+f.getFullYear();
    }
    b['date'] = g;
    b['time'] = f.getHours().GpsFormat('00') + ":" + f.getMinutes().GpsFormat('00') + ":" + f.getSeconds().GpsFormat('00');
    
    b['speed'] = c[7];
    if (b['speed'] == "") {
        b['speed'] = 0;
    }
   
    b['speed'] = parseFloat(b['speed']).toFixed(2);//parseFloat(parseFloat(b['speed']) * 1.85).toFixed(2);
    if (c[8] == "") {
        c[8] = 0;
    }
    b['direction'] = c[8];
    b['mileage'] =c[10];
    b['acc'] = c[11];
    b['door'] = c[12];
    
    b['battery'] = c[14];
    b['voltage'] = c[15];
    b['fuel'] = c[16];
    b['ad2'] = c[17];
    b['flag'] = c[18];
    
    return b;
};


function GPS() {
    this.id = -1;
    this.uin = "";
    this.lat = 0;
    this.lng = 0;
    this.date = 0;
    this.signal = 0;
    this.speed = 0;
    this.direction = 0;
    this.marker = null;
    this.target_marker = null;
    this.icon = null;
    this.src = "";
    this.same_times = 0;
    this.update_interval = 10000;
    this.cmd = 0;
    this.last_lat = 0;
    this.last_lng = 0;
    this.cur_ani = 0;
    this.ani_count = 0;
    this.address = "";
    this.line = null;
    this.points = [];
    this.lableText = null;// 用于显示跟踪器名称或者地址
    this.tracker = null;// 对应的跟踪器信息
    this.acc = 0;
    this.door = 0;
};
function GPSAlarm() {
    this.uin = "";
    this.gprmc = "";
    this.cmd = 0;
    this.realTimeDataID = 0;
};
function GPSTracker() {
    this.uin = "";
    this.model = "";
    this.name = "";
    this.plugin = "";
};
function get_arrow_img(a, b, c) {
    if (c > 0) {
        return "arrow/alarm.gif";
    }
    if (parseInt(b) == 0) {
        return "arrow/nosignal.png";
    }
    a = parseFloat(a);
    if (isNaN(a)) {
        a = 0;
    }
    var i = 15;
    var n = parseInt((a + i / 2) / i) * i;
    return "arrow/arrow_" + n + ".png";
};
function get_gps_prompt(a, b, c, d, e, f, g, h, i) {
    var j = "";
    if (b > 0) {
        var k = "gps_comment.jsp?id=" + b;
        var l = "javascript:showPopWin('" + k + "', 400, 300)";
        j = '    <div style="font-style:underline; color:blue; font-weight:bold; display:inline; cursor:hand;" onclick="' + l + '">' + g_str_comment + '</div>';
    }
    var m = "";
    var n = "";
    if (h > 0) {
        n = "  <img src='arrow/alarm.gif' />  ";
    }
    var o = "";
    if (a != "") {
        o = "<strong>" + g_str_id + "</strong>  " + a;
    }
    var p = get_gps_cmd_str(h);
    if (h > 0) {
        p = '<span style="color:red;font-weight:bold">' + get_gps_cmd_str(h) + '</span>';
    }
    var q = "<div style='line-height:16px;display:inline;'><table cellpadding=0 cellspacing=0><tr><td align='left'><strong>" + o + n + "<br />" + c + "</strong>    " + "<br>" + "<strong>" + g_str_lat + "</strong> " + d + "  <strong>" + g_str_lng + "</strong> " + e + "<br><strong>" + g_str_signal + "</strong> " + (f == 0 ? "No": "OK") + "        <strong>" + g_str_speed + "</strong> " + g + "<br><strong>" + g_str_state + "</strong> " + p + "" + m + "" + j + "<br />" + i + "</td></tr></table><div>";
    return q;
};

function get_prompt_on_map(imei, b, date, lat, lng, signal, speed, h, time){
	var j = "";
    if (b > 0) {
        var k = "gps_comment.jsp?id=" + b;
        var l = "javascript:showPopWin('" + k + "', 400, 300)";
        j = '    <div style="font-style:underline; color:blue; font-weight:bold; display:inline; cursor:hand;" onclick="' + l + '">' + g_str_comment + '</div>';
    }
    var m = "";
    var n = "";
    if (h > 0) {
        n = "  <img src='arrow/alarm.gif' />  ";
    }
    var o = "";
    if (imei != "") {
        o = (gpstracker_map[imei]  ? gpstracker_map[imei].trackerName:"");
    }
//    var p = get_gps_cmd_str(h);
//    if (h > 0) {
//        p = '';// alarm icon '<span style="color:red;font-weight:bold">' + get_gps_cmd_str(h) + '</span>';
//    }
    var q = "<div style='line-height:16px;display:inline;opacity:0.3'><table cellpadding=0 cellspacing=0><tr><td align='left'><b>" +gis_info_name_title+  ":</b>    " +o 
    + "<br /><b>" + gis_info_date_title + ":</b>    " +date
    + "<br /><b>" + gis_info_time_title + ":</b>    " +time
    + "<br/>" + "<b>" + g_str_lat + ":</b> " + lat 
    + "  <br/><b>" + g_str_lng + ":</b> " + lng + 
    "<br/>"
    + "        <b>" + g_str_speed + ":</b> " + speed 
    +   "</td></tr></table><div>";
    return q;
}

Number.prototype.GpsFormat = function(format){
	if(typeof format == 'undefined' || format == null){
		return this.Value;
	}
	format = format.toString();
	var result = this.toString();
	
	if(format.match(/^[0]+$/)){
		var s = result.toString();
		s = format + s; 
		result = s.substring(s.length - format.length, s.length);
		
	}
	
	return result;
};